﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading;
using Swirebev.PhotoDownload.Cloud;
using Swirebev.PhotoDownload.Common;
using Swirebev.PhotoDownload.DAL;
using Swirebev.PhotoDownload.Model;

namespace Swirebev.PhotoDownload.Bll
{
	public class PhotoBLL
	{
		private static object _lockObject = new object();
		private static Thread _thisThread;
		public static void StartDownload()
		{
          
            //PhotoDownload.DAL.photoTime.GetPhotoInfo();
			PhotoBLL._thisThread = new Thread(new ThreadStart(PhotoBLL.startDownload));
			PhotoBLL._thisThread.Start();
		}

		public static void StopDownload()
		{
			try
			{
				Log.Info(MethodBase.GetCurrentMethod().DeclaringType, "Waiting for the thread processing");
				Monitor.Enter(PhotoBLL._lockObject);
				Log.Info(MethodBase.GetCurrentMethod().DeclaringType, "Thread processing has been completed");

				PhotoBLL._thisThread.Abort();
				Monitor.Exit(PhotoBLL._lockObject);
				Log.Info(MethodBase.GetCurrentMethod().DeclaringType, "The thread has stopped");

				Log.Info(MethodBase.GetCurrentMethod().DeclaringType, "Success to stop service");
			}
			catch (Exception ex)
			{
				Log.Error(MethodBase.GetCurrentMethod().DeclaringType, "Failed to stop service", ex);
			}
		}

        //public static void Waite()
        //{
        //    PhotoBLL._thisThread.Join();
        //}

        private static void startDownload()//开始下载照片
		{
            //while (true)
            //{
                try
                {
                    Monitor.Enter(PhotoBLL._lockObject);
                  
                    //List<PhotoData> photoListTask = AVOSCloudHelper.Instance.DownloadPhotos();//下载照片到目录
                    List<PhotoData> photoListTask = QiniuCloudHelper.Instance.DownloadPhotos();
                    
                    if (photoListTask != null)
                    {
                        ////Log.Info(MethodBase.GetCurrentMethod().DeclaringType, photoListTask.Count + " photo");

                        foreach (PhotoData photoData in photoListTask)//将照片保存到OC服务端
                        {
                            Log.Info(MethodBase.GetCurrentMethod().DeclaringType, photoData.ObjectId);
                            SavePhotoInfo(photoData);
                            //if (SavePhotoInfo(photoData))
                            //{
                                //bool t = AVOSCloudHelper.Instance.UpdateDownloadSuccess(photoData.ObjectId);//将云端记录删除
                                //bool t = QiniuCloudHelper.Instance.UpdateDownloadSuccess(photoData.Key, photoData.ObjectId);//将云端记录删除
                            //}
                        }
                    }
                    Monitor.Exit(PhotoBLL._lockObject);
                    //Thread.Sleep(Settings.Instance.DownloadSleep);
                }
                catch (Exception ex)
                {
                    Log.Error(MethodBase.GetCurrentMethod().DeclaringType, ex.Message, ex);
                }
			//}
		}

		/// <summary>`
		/// 保存照片信息
		/// </summary>
		/// <param name="photo"></param>
		/// <returns></returns>
		public static bool SavePhotoInfo(PhotoData photo)
		{
			if (photo != null)
				return PhotoDataAccess.SavePhotoInfo(photo);
			else
				return false;
		}
	}
}
